// 全局 数据对象
const data = {
  pagenum: 1,
  pagesize: 100,
  cate_id: '',
  state: '',
}
const table = {
  data: []
}
renderTable()
// 请求页面 并 渲染
function renderTable () {
  // 请求数据
  $.ajax({
    url: '/my/article/list',
    data
  }).then(res => {
    const {status, message, data} = res
    if (status === 0) {
      layui.layer.msg(message)
      table.data = data
      layui.table.render({
        elem: '#demo',
        // url: 'http://api-breakingnews-web.itheima.net/my/article/list', //数据接口
        limit: 2,
        limits: [2, 3, 5, 10],
        page: true, //开启分页
        data: table.data,
        cols: [
          [
            //表头
            { field: 'Id', title: '类别id', hide: true},
            { field: 'title', title: '文章标题'},
            { field: 'cate_name', title: '分类'},
            { field: 'pub_date', title: '发表时间'},
            { field: 'state', title: '状态'},
            { field: 'operation', title: '操作'}
          ],
        ],
      })
    } else {
      layui.layer.msg(message)
    }
  })
}
renderCate()
function renderCate() {
  $.ajax({
    url: '/my/article/cates',
  }).then(res => {
    const {status, message} = res
    if (status === 0) {
      layui.layer.msg(message)
      const htmlStr = template('cate_tbody', res)
      $('[name="cate_id"]').html(htmlStr)
      layui.form.render(null, 'cate_form')
    } else {
      layui.layer.msg(message)
    }
  })
}
// 筛选
$('.btn_filter').on('click', function(){
  const val = layui.form.val('cate_form')
  data.cate_id = val.cate_id
  data.state = val.state
  renderTable()
})